package com.zjbti.note;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zjbti.note.utils.DBUtils;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv_time,tv_main_title,tv_back;
    private EditText et_title,et_content;
    private ImageView iv_del,iv_save;
    private DBUtils dbUtils;
    private int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        initView();
        initData();
    }

    private void initView() {
        tv_back=findViewById(R.id.tv_back);
        tv_back.setVisibility(View.VISIBLE);
        et_title=findViewById(R.id.et_title);
        et_content=findViewById(R.id.et_content);
        tv_time=findViewById(R.id.tv_time);
        iv_del=findViewById(R.id.iv_del);
        iv_save=findViewById(R.id.iv_save);
        tv_main_title=findViewById(R.id.tv_main_title);
        tv_back.setOnClickListener(this);
        iv_del.setOnClickListener(this);
        iv_save.setOnClickListener(this);
    }
    private void initData(){
        dbUtils=new DBUtils(this);
        Intent intent=getIntent();
        id=intent.getIntExtra("id",0);
        if(id!=0){
            tv_main_title.setText("修改记录");
            et_title.setText(intent.getStringExtra("title"));
            et_content.setText(intent.getStringExtra("content"));
            tv_time.setText(intent.getStringExtra("time"));
            tv_time.setVisibility(View.VISIBLE);
        }else{
            tv_main_title.setText("添加记录");
            tv_time.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.iv_del:
                et_content.setText("");
                break;
            case R.id.iv_save:
                String title=et_title.getText().toString().trim();
                String content=et_content.getText().toString().trim();
                if(id!=0) {
                    if (content.length() > 0) {
                        if (dbUtils.updateNote(id, title, content, DBUtils.getTime())) {
                            showToast("修改成功");
                            setResult(2);
                            finish();
                        } else {
                            showToast("修改失败");
                        }
                    } else {
                        showToast("修改内容不能为空");
                    }
                }else{
                            if(content.length()>0) {
                                if (dbUtils.saveNote(title, content, DBUtils.getTime())) {
                                    showToast("保存成功");
                                    setResult(2);
                                    finish();
                                } else {
                                    showToast("保存失败");
                                }
                            }else{
                                showToast("添加的内容不能为空");
                            }
                        }
                        break;
                    }
                }
                public void showToast(String message){
                    Toast.makeText(RecordActivity.this,message,Toast.LENGTH_SHORT).show();
                }
        }

